/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * usuario.java
 *
 * Created on 21-nov-2011, 15:32:54
 */
package formulario;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author ALEX DARY JULIAN
 */
public class user extends javax.swing.JFrame {
 
      cuestionario c =new cuestionario();
      Connection co=null;
      String kk, abc;
      int col;
      ResultSet re;
      boolean existecarnet=false;
    /** Creates new form usuario */
    public user() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        user = new javax.swing.JTextField();
        code = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        mate = new javax.swing.JMenuItem();
        info = new javax.swing.JMenuItem();
        lite = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Login");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setFont(new java.awt.Font("Centaur", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 0));
        jLabel1.setText("Usuario");

        jLabel2.setFont(new java.awt.Font("Centaur", 1, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(51, 51, 0));
        jLabel2.setText("Codigo");

        jMenu1.setText("Pruebas");
        jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resueltas(evt);
            }
        });

        jMenu3.setText("Realizar prueba");

        mate.setText("Matematica");
        mate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                matematicaActionPerformed(evt);
            }
        });
        jMenu3.add(mate);

        info.setText("Informatica");
        info.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                informaticaActionPerformed(evt);
            }
        });
        jMenu3.add(info);

        lite.setText("Literatura");
        lite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                literaturaActionPerformed(evt);
            }
        });
        jMenu3.add(lite);

        jMenu1.add(jMenu3);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(code, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                    .addComponent(user, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE))
                .addGap(27, 27, 27))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(code, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        code.addKeyListener(new KeyAdapter()
            {
                public void keyTyped(KeyEvent e)
                {
                    char caracter = e.getKeyChar();

                    // Verificar si la tecla pulsada no es un digito
                    if(((caracter < '0') ||
                        (caracter > '9')) &&
                    (caracter != '\b' /*corresponde a BACK_SPACE*/))
                {
                    e.consume();  // ignorar el evento de teclado
                }
            }
        });

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void literaturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_literaturaActionPerformed
kk="literatura";
col=5;
abc="respuestaliteratura";
lite.setEnabled(false);
verifica(); 

}//GEN-LAST:event_literaturaActionPerformed

private void informaticaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_informaticaActionPerformed
kk="informatica";
col=4;
abc="respuestainformatica";
info.setEnabled(false);
verifica();

}//GEN-LAST:event_informaticaActionPerformed

private void matematicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_matematicaActionPerformed
kk="matematicas";
col=3;
abc="respuestamatematica";
mate.setEnabled(false);
verifica();
}//GEN-LAST:event_matematicaActionPerformed

private void resueltas(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resueltas
    
      if(user.getText().isEmpty() ||code.getText().isEmpty()){
     JOptionPane.showMessageDialog(null, "TODOS los campos son obligatorios","Error",JOptionPane.ERROR_MESSAGE);                                                                                                            
                                                              }
      else 
         { try 
            {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            co = DriverManager.getConnection("jdbc:odbc:alex");
            Statement st=co.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            re=st.executeQuery("SELECT * FROM usuario where codigo = "+code.getText()+"");
            re.last();
            //re.updateString(2,nasasa);
            int existe=re.getRow();
            
             
            if(existe>0){
            BigDecimal w=re.getBigDecimal(4);
            BigDecimal x=re.getBigDecimal(5);
            BigDecimal y=re.getBigDecimal(6);
              existecarnet=true;
              if(w!=null){
              mate.setEnabled(false);
                                         }
              if(x!=null){
              info.setEnabled(false);
                                      }
              if(y!=null){
              lite.setEnabled(false);
                                      }
                        
                         }
               if(mate.isEnabled()==false && info.isEnabled()==false && lite.isEnabled()==false){
               JOptionPane.showMessageDialog(null, "Usted ya ha realizado todas las pruebas");
               this.dispose();
                                                                                                }
        
        } catch(Exception ex)
        {
            ex.printStackTrace();
        }  }
}//GEN-LAST:event_resueltas
public void verifica(){
  if(user.getText().isEmpty() ||code.getText().isEmpty()){
     JOptionPane.showMessageDialog(null, "TODOS los campos son obligatorios","Error",JOptionPane.ERROR_MESSAGE);                                                                                                            
  }
  else{
  user.setEnabled(false);
  code.setEnabled(false);
  c.setVisible(true);
  c.setdatos(user.getText(),code.getText(),kk,col,abc,existecarnet);
  
     }
                      }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(user.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(user.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(user.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(user.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new user().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField code;
    private javax.swing.JMenuItem info;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem lite;
    private javax.swing.JMenuItem mate;
    private javax.swing.JTextField user;
    // End of variables declaration//GEN-END:variables
}
